package com.evil.application.util;

import com.evil.application.enums.form.control.ControlCodeEnum;
import com.evil.application.mapper.ApplicationInfoMapper;
import com.evil.application.mapper.ApplicationReportingMapper;
import com.evil.application.pojo.dto.form.control.code.config.FilterConfigDTO;
import com.evil.application.pojo.dto.permission.control.ControlPermissionDTO;
import com.evil.application.pojo.dto.reporting.ApplicationReportingDTO;
import com.evil.application.pojo.entity.ApplicationInfo;
import com.evil.application.pojo.entity.ApplicationReporting;
import com.evil.application.service.*;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.application.dto.form.control.ControlBaseDTO;
import com.evil.common.application.dto.judge.JudgeOr;
import com.evil.common.application.dto.judge.JudgeSourceDTO;
import com.evil.common.application.dto.permission.app.AppPermissionSourceDTO;
import com.evil.common.application.dto.permission.control.ControlPermissionSourceDTO;
import com.evil.common.application.enums.ApplicationSourceTypeEnum;
import com.evil.common.application.enums.form.ControlPermissionSourceTypeEnum;
import com.evil.common.application.enums.judge.JudgeSourceTypeEnum;
import com.evil.common.core.enums.SwitchEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 应用报表工具
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Component
@AllArgsConstructor
public class ApplicationReportingUtil {

    private final LoginUtil loginUtil;

    private final ApplicationReportingMapper applicationReportingMapper;

    private final ApplicationInfoMapper applicationInfoMapper;

    private final ApplicationFormControlService applicationFormControlService;

    private final ApplicationJudgeService applicationJudgeService;

    private final ApplicationPermissionService applicationPermissionService;

    private final ControlPermissionService controlPermissionService;

    public ApplicationReportingDTO appReportingInfo(String reportingKey) {
        ApplicationReporting reporting = applicationReportingMapper.findByKey(reportingKey);
        Long reportingId = reporting.getReportingId();

        ApplicationInfo applicationInfo = applicationInfoMapper.findById(reporting.getApplicationId());
        // 检查企业
        loginUtil.checkEnterpriseId(applicationInfo.getEnterpriseId());

        Map<Long, ControlBaseDTO> controlBaseMap = applicationFormControlService.findReleaseBaseMapByApp(applicationInfo, true);

        // 检查报表权限
        applicationPermissionService.loginUserHasPermission_Check(
                applicationInfo.getEnterpriseId(),
                reporting.getPermissionStatus(),
                new AppPermissionSourceDTO(ApplicationSourceTypeEnum.REPORTING, reportingId),
                false);

        // 判断控件是否属于表格子控件
        Function<Long, Boolean> beingTableChild = controlId -> {
            if (controlBaseMap.containsKey(controlId)) {
                ControlBaseDTO controlBase = controlBaseMap.get(controlId);
                if (!ApplicationUtil.DEFAULT_PARENT_CONTROL_ID.equals(controlBase.getParentControlId())) {
                    if (controlBaseMap.containsKey(controlBase.getParentControlId())) {
                        ControlBaseDTO parentControlBase = controlBaseMap.get(controlBase.getParentControlId());
                        return ControlCodeEnum.TABLE.getCode().equals(parentControlBase.getControlCode());
                    }
                }
            }
            return false;
        };
        ApplicationReportingDTO reportingDTO = new ApplicationReportingDTO(reporting, beingTableChild);

        // 筛选条件
        if (SwitchEnum.isOpen(reporting.getFilterStatus())) {
            List<JudgeOr> judgeOrs = applicationJudgeService.findDTOBySource(
                    new JudgeSourceDTO(JudgeSourceTypeEnum.REPORTING_FILTER_CONDITION, reportingId));
            FilterConfigDTO filterConfigDTO = new FilterConfigDTO();
            filterConfigDTO.setJudgeOrs(judgeOrs);
            reportingDTO.setFilterConfig(filterConfigDTO);
        }

        // 控件权限
        List<ControlPermissionDTO> controlPermissions = controlPermissionService.findBasesBySource(
                new ControlPermissionSourceDTO(ControlPermissionSourceTypeEnum.REPORTING, reportingId, controlBaseMap.keySet()));
        reportingDTO.setControlPermissions(controlPermissions);
        return reportingDTO;
    }
}
